﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CMN.Models;
using CMN.Entity;
using CMN.Config;
using CMN.Common;
using CMN.Models.Interfaces;
using System.Web;
using CMN.BLL;
using System.Threading;

namespace CMN.Forms
{
    public partial class frmMain : Form
    {
         private AsyncUpdateUrlLog m_AsyncImport;

      
      

       
        public frmMain()
        {
            InitializeComponent();

            string strTemp = "";
            QueryParam qp = new QueryParam();
            qp.ReturnFields = "Id,SiteName,SiteUrl,SiteType,PR,BR,IndexNum,SendUrlType,LoginName,Mark,(select count(1) from SendUrlLog where SiteId=SiteInfo.id) as UrlCount,(select count(1) from SendUrlLog where SiteId=SiteInfo.id and isindex='是') as UrlIndexCount  ";
   
            bingGridSite(qp);
            dateEnd.Text = DateTime.Now.AddDays(1).ToString();
        }

        private void tsMenuAddSite_Click(object sender, EventArgs e)
        {
            frmAddSite frmAdd = new frmAddSite();
            frmAdd.Show();
        }
        private void bingGridSite(QueryParam qp)
        {
            gridSite.Rows.Clear();
            SiteInfoEntity site = new SiteInfoEntity();
            IDataService<SiteInfoEntity> siteSv = ServiceBuilder.BuildSiteInfoService();
            int recordNum = 0;
            List<SiteInfoEntity> siteList = siteSv.List(qp, out recordNum);
            int index = 0;
            DataGridViewRow Row = null;
            int indexNum = 0;
            foreach (SiteInfoEntity info in siteList)
            {
                index = this.gridSite.Rows.Add();
                Row = this.gridSite.Rows[index];
                Row.Cells["ID"].Value = info.Id.ToString();
                Row.Cells["SiteName"].Value = info.SiteName;
                Row.Cells["SiteUrl"].Value = info.SiteUrl;

                Row.Cells["BR"].Value = info.BR.ToString();
                Row.Cells["IndexNum"].Value = info.IndexNum.ToString();
                Row.Cells["LoginName"].Value = info.LoginName;
                Row.Cells["Mark"].Value = info.Mark;
                Row.Cells["PR"].Value = info.PR.ToString();
                Row.Cells["SiteType"].Value = info.SiteType;
                Row.Cells["SendUrlType"].Value = info.SendUrlType;
                if (info.SendUrlType == "不能")
                    Row.Cells["SendUrlType"].Style.BackColor = Color.Red;
                Row.Cells["UrlCount"].Value = info.UrlCount.ToString();
                if (info.UrlCount == 0)
                {
                    Row.Cells["UrlIndexCount"].Value = "0";
                }
                else
                {
                    indexNum = (info.UrlIndexCount * 100 / info.UrlCount);
                    Row.Cells["UrlIndexCount"].Value = indexNum.ToString() + "%___" + info.UrlIndexCount.ToString();
                    if (indexNum < 40)
                    {
                        Row.Cells["UrlIndexCount"].Style.BackColor = Color.Yellow;
                    }
                }
            }
            DataGridViewTextBoxColumn dgv_Text = new DataGridViewTextBoxColumn();
            for (int i = 0; i < gridSite.Rows.Count; i++)
            {
                int j = i + 1;
                gridSite.Rows[i].HeaderCell.Value = j.ToString();
            }
            //gridSite.Rows.Add(Row);
            //gridSite.DataSource = siteList;
            //DataGridViewColumn column=new DataGridViewColumn();
            //column.HeaderText="网站名称";
            //column.Name = "SiteName";
            //column.Width
            //this.gridSite.Columns.Add( "SiteName", "网站名称");
            //this.gridSite.Columns.Add("SiteUrl", "网站URL");
            //this.gridSite.Columns.Add("PR", "PR");
            //this.gridSite.Columns.Add("BR", "BR");
            //this.gridSite.Columns.Add("IndexNum", "索引数");
            //this.gridSite.Columns.Add("SendUrlType", "发送连接类型");
            //this.gridSite.Columns.Add("UrlCount", "url数量");
            //this.gridSite.Columns.Add("UrlIndexCount", "url索引数量");
            //this.gridSite.Columns.Add("LoginName", "登陆名");
            //this.gridSite.Columns.Add("Mark", "备注");
        }

        private void gridSite_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //Clipboard.SetText(gridSite[e.ColumnIndex, e.RowIndex].Value.ToString());
        }

        private void gridSite_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
             if (e.ColumnIndex ==2)
            {
                System.Diagnostics.Process.Start("chrome.exe", this.gridSite.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
            }
        }

        private void tsMenuAddUrl_Click(object sender, EventArgs e)
        {
            frmAddUrlLog frmUrl = new frmAddUrlLog();
            frmUrl.Show();
        }

        private void btnUpdateIndex_Click(object sender, EventArgs e)
        {

            BLLHelper bllHelper = new BLLHelper();
            string strDateStart = bllHelper.GetSqlDateTime(Utils.StringToDataTime(dateStart.Text));
            string strDateEnd = bllHelper.GetSqlDateTime(Utils.StringToDataTime(dateEnd.Text));
            string strWhere = string.Concat("adddate >=", strDateStart, " and adddate<=", strDateEnd, " and isIndex!='是'");
            this.m_AsyncImport = new AsyncUpdateUrlLog(this);
            this.m_AsyncImport.TaskProgress += new EventHandler(AsyncImport_TaskProgress);
            this.m_AsyncImport.TaskDone += new EventHandler(AsyncImport_TaskDone);
            m_AsyncImport.StrWhere = strWhere;
            m_AsyncImport.Start();
            btnUpdateIndex.Enabled = false;

        }
        void AsyncImport_TaskDone(object sender, EventArgs e)
        {
            btnUpdateIndex.Enabled = true;
            lbUpdate.Text = "完成";
        }
        void AsyncImport_TaskProgress(object sender, EventArgs e)
        {
            AsyncEventArgs pAsyncEventArgs = e as AsyncEventArgs;
            int step = (int)pAsyncEventArgs.getObjects[0];
            lbUpdate.Text = "共" + pAsyncEventArgs.action.ToString() + "当前第" + (step + 1).ToString();

        }
        private void btnSearch_Click(object sender, EventArgs e)
        {
           
            QueryParam qp = new QueryParam();
            BLLHelper bllHelper = new BLLHelper();
            string strDateStart = bllHelper.GetSqlDateTime(Utils.StringToDataTime(dateStart.Text));
            string strDateEnd = bllHelper.GetSqlDateTime(Utils.StringToDataTime(dateEnd.Text));
            string strWhere = string.Concat("  adddate >=", strDateStart, " and adddate<=", strDateEnd, " ");
            qp.ReturnFields = "Id,SiteName,SiteType,SiteUrl,PR,BR,IndexNum,SendUrlType,LoginName,Mark,(select count(1) from SendUrlLog where SiteId=SiteInfo.id and " + strWhere + " ) as UrlCount,(select count(1) from SendUrlLog where SiteId=SiteInfo.id and isindex='是'  and " + strWhere + " ) as UrlIndexCount  ";
            if (tbKeyWord.Text != "")
                qp.Where = string.Concat(" SiteName like '%", tbKeyWord.Text, "%' or SiteUrl like '%", tbKeyWord.Text, "%' ");

            bingGridSite(qp);
        }

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源，为 true；否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            //停止工作线程
            if( this.m_AsyncImport!=null)
            this.m_AsyncImport.Stop();
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        

        private void gridSite_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1 && this.gridSite.Rows.Count > e.RowIndex)
            {
                frmAddSite frmUrl = new frmAddSite(Utils.StringToInt(this.gridSite.Rows[e.RowIndex].Cells[0].Value.ToString(), 0));

                frmUrl.Show();
            }
        }

        private void tsMenuUrlLogSearch_Click(object sender, EventArgs e)
        {
            frmSearchUrlLog searchUrl = new frmSearchUrlLog();
            searchUrl.Show();
        }

        private void 查看所有连接ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmSearchUrlLog frmSearchUrl = new frmSearchUrlLog(Utils.StringToInt(gridSite.Rows[gridSite.CurrentCell.RowIndex].Cells[0].Value.ToString(), 0));
            frmSearchUrl.Show();
           
        }
        

      
    }
}
